package cn.biplam.front.component.data.tree.treebase;

import java.util.Map;

import cn.biplam.common.paramters.TreeParam;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.front.component.data.tree.treebase.Tree_Type.Tree_NodeType_Commen;
import cn.biplam.front.component.data.tree.treebase.Tree_Type.Tree_PicType;
import cn.biplam.front.component.data.tree.treebase.img.PlusMinus;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.template.control.TreeNew;
import cn.biplam.front.template.control.TreeNew.TreeID_Index;

/**
 * 生成树时用到得回调函数接口
 *  1.根据取树的类型，返回当前树的索引的operatorId
 *  2.根据树的节点类型和是否是叶子节点返回节点图片名称(图片默认为蓝色系列)
 *  3.根据树的节点类型、是否是叶子节点、节点ID 返回节点的左键脚本
 * @author WangSong 2009-6-5下午03:11:34
 * @version 1.0
 */
public class TreeCallBackNew{

	/** 左键action*/
	private KvMap<String, String> leftActionIds;
	/** 左键目标容器*/
	private KvMap<String, String> leftTargetIds;
	/** 左键的操作类型*/
	private KvMap<Tree_NodeType_Commen,String> OperatorTypes;
	/** 图片设置*/
	private TreeCallBackImg callBackImg;
	/** 顶级页面id*/
	protected String pageId;

	public TreeCallBackNew() throws ErrorMessage {
		callBackImg = TreeCallBackImg.getInstanct(Tree_PicType.Blue) ;
		OperatorTypes = new KvMap<Tree_NodeType_Commen,String>();
		leftActionIds = new KvMap<String, String>();
		leftTargetIds = new KvMap<String, String>();
	}

	/**
	 * 设置左键信息
	 * @param OperatorType 节点操作类型
	 * @param actionId 目标行为id
	 * @param TargetId 目标容器id
	 * @throws ErrorMessage 
	 */
	public void putLeftClick(Tree_NodeType_Commen OperatorType,String actionId,String TargetId) throws ErrorMessage{
		putLeftClick(OperatorType.toString(), actionId, TargetId);
	}

	/**
	 * 设置左键信息
	 * @param OperatorType 节点操作类型
	 * @param actionId 目标行为id
	 * @param TargetId 目标容器id
	 * @throws ErrorMessage 
	 */
	public void putLeftClick(String OperatorType,String actionId,String TargetId) throws ErrorMessage{
		if(!TOOLS.StringIsEmpty(actionId) && !TOOLS.StringIsEmpty(TargetId)){
			OperatorTypes.put(Tree_NodeType_Commen.getValue(OperatorType), OperatorType);
			leftActionIds.put(OperatorType, actionId);
			leftTargetIds.put(OperatorType, TargetId);
		}
	}

	/**
	 * 设置树图片设置类
	 * @param callBackImg
	 */
	public void setPic(TreeCallBackImg callBackImg){
		if(callBackImg!=null){
			this.callBackImg = callBackImg;
		}
	}



	/**
	 * 根据树的节点类型、是否是叶子节点、节点ID、节点级别等 返回节点的左键脚本
	 * @param pcId 调用树VC的容器id
	 * @param operatorType 操作类型
	 * @param isLeafs 是否是叶子节点
	 * @param nodeId 节点容器id
	 * @param level 级别
	 * @param levelDown 向下取几级
	 * @param showAddFlag 是否显示加号标志
	 * @param showCheckBox 是否显示复选框标志
	 * @param tableName 表名
	 * @param treeAid 树VC的actionId
	 * @param nodeName 节点名称
	 * @return 左键脚本
	 */
	public String getLeftClickScript(String pcId,
			String operatorType, 
			boolean isLeafs,
			String nodeId, 
			int level, 
			int levelDown,
			boolean showAddFlag,
			boolean showCheckBox,
			String tableName,
			String treeAid,
			String nodeName) throws ErrorMessage {

		StringBuilder sp = new StringBuilder();
		//otherContainer = HiddenParams.hiddenId+nodeId.split(",")[0];
		/**不是叶子节点 绑定"展开"和"收起"动作*/
		if(!isLeafs){
			this._bindExtend(sp, nodeId, level, levelDown,showAddFlag);
		}
		//绑定checkbox点击事件
		if(TOOLS.stringToInt(operatorType)>=3 && showCheckBox){
			String name = nodeId.split(",")[0]+"ckb";
			String cId = name+","+nodeId.split(",")[TreeNew.TreeID_Index.NodeID.toInt()];
			sp.append(_getBindScript(cId, name));
		}
		String[] fullArr = nodeId.split(",");
		//设置隐藏参数
		String treeHidden = "hd"+fullArr[0]+fullArr[TreeID_Index.NodeType.toInt()];
		bindLeftClickNodeInfo(sp,nodeId,treeHidden,pcId,tableName,treeAid,nodeName);
		if(this.OperatorTypes.containsKey(Tree_NodeType_Commen.getValue(operatorType))){
			//绑定左键的指定目标区域刷新动作,realId
			this.bindRefresh(sp, 
					nodeId.split(",")[7],
					getLeftClickActionId(operatorType),
					getLeftClickTargetId(operatorType),
					treeHidden);
		}
		//绑定背景颜色脚本
		sp.append("changeBg($(this.id));");
		return sp.toString();
	}

	/**
	 * 根据树的节点类型、是否是叶子节点、节点ID、节点级别等 返回节点的图片脚本
	 * @param operatorType 节点类型
	 * @param isLeaf 是否是叶子节点
	 * @param nodeId 节点ID
	 * @param level 节点级别
	 * @param levelDown 树向下显示的级别
	 * @return String 节点图片名称
	 */
	public String getPicScript(String operatorType, 
			boolean isLeafs,
			String nodeId, 
			int level, 
			int levelDown,
			boolean showAddFlag) throws ErrorMessage {

		StringBuilder sp = new StringBuilder();
		/**不是叶子节点 绑定"展开"和"收起"动作*/
		if(!isLeafs){
			this._bindExtend(sp, nodeId, level, levelDown,showAddFlag);
		}

		return sp.toString();
	}

	public String getNodeOnMouseOver(String operatorType) throws ErrorMessage{
		if(this.OperatorTypes.containsKey(Tree_NodeType_Commen.getValue(operatorType))){
			return "onNodeL(this);";
		}else{
			return "onNode(this)";
		}
	}

	/**
	 * 取左键的目标容器id
	 * @param operatorType 节点类型
	 * @return String 左键的目标容器id
	 */
	public String getLeftClickTargetId(String operatorType) {
		if(getLeftClickTargetId()!=null){
			return getLeftClickTargetId().getByKey(operatorType);
		}
		return null;
	}

	/**
	 * 取左键的行为id
	 * @param operatorType 节点类型
	 * @return String 左键的行为id
	 */
	public String getLeftClickActionId(String operatorType) {
		if(getLeftClickActionId()!=null){
			return getLeftClickActionId().getByKey(operatorType);
		}
		return null;
	}

	/**
	 * 取所有左键的行为id(格式为:operatorType1#actionId1<operatorType2#actionId2)
	 * @param operatorType 节点操作类型
	 * @return String 左键的行为id
	 */
	public String getLeftClickActionIdAll() {
		return getLeftString(leftActionIds);
	}

	/**
	 * 取所有左键的目标容器id(格式为:operatorType1#targetId1<operatorType2#targetId2)
	 * @param operatorType 节点操作类型
	 * @return String 左键的目标容器id
	 */
	public String getLeftClickTargetIdAll() {
		return getLeftString(leftTargetIds);
	}

	/**
	 * 根据树的节点类型和是否是叶子节点返回节点图片名称
	 * @param operatorType  节点操作类型
	 * @param nodeType 节点类型.此节点类型的节点显示当前设置的图片,用于设置同一操作类型节点(operatorType)的不同图片显示
	 * @param isLeaf  是否是叶子节点
	 * @return  节点图片名称
	 */
	public String getNodePic(String operatorType,
			String nodeType,
			boolean isLeaf)
	throws ErrorMessage {
		return this.callBackImg.get(operatorType, nodeType, isLeaf);
	}

	/**
	 * 取虚根的图片
	 * @return 虚根图片
	 */
	public String getVirNodePic(){
		return this.callBackImg.getVir();
	}

	public PlusMinus getPlusMinus() throws ErrorMessage {
		return callBackImg.getPicPlusMinus();
	}

	/**
	 * 取节点文本页面对象
	 * @param nodeId 				节点id
	 * @param operatorType 			节点类型
	 * @param realId 				真实Id
	 * @param text 					节点名称文本
	 * @param customerParamValue 	自定义参数值(整棵树的参数容器)
	 * @return HtmlWindow
	 */
	public HtmlWindow getText(String nodeId, String operatorType,String realId,String text, KvMap<String,String> otherParams)  throws ErrorMessage {
		return null;
	}

	/**
	 * 绑定左键的"展开"和"收起"动作
	 * @throws ErrorMessage 
	 */
	private void _bindExtend(StringBuilder sp,String nodeId,int level,int levelDown,boolean showAddFlag) throws ErrorMessage{
		//拼接图片id 结构为:aid,nodeId,nodeType
		String imageID = this.getImageIdByNodeId(nodeId, level, levelDown,showAddFlag);
		if(!showAddFlag && level==1){
			sp.append("tkr('");
		}else{
			sp.append("tk('");
		}
		sp.append(imageID);
		sp.append("','");
		sp.append(nodeId.split(",")[0]);
		sp.append("');");
	}

	/**
	 * 根据nodeId取节点的图片id
	 * @param nodeId  节点id
	 * @param level   节点级别
	 * @param levelDown  从当前节点向下取几级
	 * @return  图片名称
	 * @throws ErrorMessage
	 */
	private String getImageIdByNodeId(String nodeId,int level,int levelDown,boolean showAddFlag) throws ErrorMessage{
		String imageId = "";
		//节点图片标志 c:展开图片，及加号  e为收起图片，及减号
		String imageFlag = "c";
		//当前节点级别小于要向下展开的级别时，隐藏的图片是 加号图片，点击时的图片则是 减号
		if(level<levelDown){
			imageFlag = "e";
			if(!showAddFlag){
				imageFlag = "s";
			}
		}
		String[] arrNodeId = nodeId.split(",");
		if(arrNodeId!=null && arrNodeId.length>3){
			imageId = nodeId +","+ imageFlag;
		}else{
			throw new ErrorMessage("TreeCallBackBase.java,nodeId is incorrect");
		}
		return imageId;
	}

	/**
	 * 绑定更新左键节点信息(节点id、树索引、节点类型、节点真实id)
	 * @param fullNodeId 节点容器id(包括节点的所有信息)
	 * @param nodeId 节点id
	 * @param treeIndex 树索引
	 * @param operatorType 节点类型
	 * @param realId 节点真实id
	 */
	private void bindLeftClickNodeInfo(StringBuilder sp,
			String fullNodeId,
			String treeHidden,
			String pcId,
			String tableName,
			String treeAid,
			String nodeName){
		String[] fullArr = fullNodeId.split(",");
		String nodeId = fullArr[TreeID_Index.NodeID.toInt()];
		String treeIndex = fullArr[TreeID_Index.NodeType.toInt()];
		String operatorType = fullArr[TreeID_Index.OperatorType.toInt()];
		String realId = fullArr[TreeID_Index.NodeRealId.toInt()];

		sp.append("$('"+treeHidden+","+TreeParam.NODE_ID+"').value='"+nodeId+"';");
		sp.append("$('"+treeHidden+","+TreeParam.TREE_INDEX+"').value='"+treeIndex+"';");
		sp.append("$('"+treeHidden+","+TreeParam.OPERATOR_TYPE+"').value='"+operatorType+"';");
		sp.append("$('"+treeHidden+","+TreeParam.NODE_REAL_ID+"').value='"+realId+"';");
		sp.append("$('"+treeHidden+","+TreeParam.TREE_PCID+"').value='"+pcId+"';");
		sp.append("$('"+treeHidden+","+TreeParam.TABLE_NAME+"').value='"+tableName+"';");
		sp.append("$('"+treeHidden+","+TreeParam.TREE_AID+"').value='"+treeAid+"';");
		sp.append("$('"+treeHidden+","+TreeParam.NODE_NAME+"').value='"+nodeName+"';");

	}

	/**
	 * 绑定左键刷新目标区域脚本
	 * @param sp 脚本字符串
	 * @param nodeId 节点id
	 * @param actionId 左键行为id
	 * @param targetId 左键目标id
	 * @param treeHidden 隐藏容器名称
	 */
	protected void bindRefresh(StringBuilder sp,
			String nodeId,
			String actionId,
			String targetId,
			String treeHidden){
		if(actionId!=null && targetId!=null){
			sp.append("tln(this.id,'"+actionId+"','"+nodeId+"','"+targetId+"','"+treeHidden+"');");
		}
	}

	/**
	 * 取左键的行为id
	 * @return KvMap<OperatorType,ActionId>
	 */
	private KvMap<String, String> getLeftClickActionId() {
		return leftActionIds;
	}

	/**
	 * 取左键的目标容器id
	 * @return KvMap<OperatorType,TargetId>
	 */
	private KvMap<String, String> getLeftClickTargetId() {
		return leftTargetIds;
	}

	/**
	 * 取左键actionId或targetId字符串
	 * @param left actionId或targetId容器
	 * @return 左键actionId或targetId字符串
	 */
	private String getLeftString(KvMap<String,String> left){
		StringBuilder sb = new StringBuilder();
		int i=0;
		for(Map.Entry<Tree_NodeType_Commen, String>operatorType:OperatorTypes){
			if(i>0){
				sb.append("<");
			}
			sb.append(operatorType.getKey().toString());
			sb.append("#");
			sb.append(left.getByKey(operatorType.getKey().toString()));
			i++;
		}
		return sb.toString();
	}

	/**
	 * 绑checkbox脚本
	 * @param thisDivID 图片id
	 * @param cId 容器id
	 * @param name checkbox名称
	 * @return 点击脚本
	 */
	private String _getBindScript(String cId,String name){
		StringBuilder sb = new StringBuilder();
		sb.append("cbkTr('");
		sb.append(cId);
		sb.append("','");
		sb.append(name);
		sb.append("');");
		return sb.toString();
	}

}
